package com.drojian.root;

import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.drojian.demo.R;

import com.drojian.root.database.SqlUser;


public class LoginFragment extends Fragment implements View.OnClickListener {
    private EditText passWord,userName;
    private boolean isPasswordVisible = false; // 用于追踪密码的显示状态
    private Button login,register;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.root_fragment_login, container, false);
        userName = view.findViewById(R.id.editTextText);
        passWord = view.findViewById(R.id.editTextText2);
        login = view.findViewById(R.id.button_1111);
        register = view.findViewById(R.id.button_2222);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_1111:
                SqlUser user1 = new SqlUser(getContext(), "user.db", null, 1);
                SQLiteDatabase db = user1.getWritableDatabase();
                Cursor user = db.query("user", null, "username = ?",new String[]{userName.getText().toString()}, null, null, null);
                if(user != null && user.moveToFirst()){
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    intent.putExtra("username",userName.getText().toString());
                    startActivity(intent);
                    Toast.makeText(getContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
                if(user != null && !user.isClosed() ){
                    user.close();
                }
                break;
            case R.id.button_2222:
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.add(R.id.root_fragment,new RegisterFragment());
                transaction.addToBackStack(null);
                transaction.hide(this);
                transaction.commit();
                break;
        }
    }

    public static LoginFragment newInstance() {
        return new LoginFragment();
    }



}